import {updateApp} from '@/request/Api/update.js'

export const update = () =>{
	
	// #ifdef APP
	plus.runtime.getProperty(plus.runtime.appid,function(widgetinfo) {
		updateApp({version:widgetinfo.version}).then((res)=>{
			const data = res.data
			if(!data.url)
			{
				return uni.showToast({
					title:'已经是最新版本',
					icon:'none'
				})
			}
			uni.showModal({
				title: '发现新版本'+data.version,
				content: '是否更新',
				showCancel: true,
				cancelText: '暂不更新',
				confirmText: '更新',
				success: res => {
					if (res.confirm) {
						uni.downloadFile({
							url: data.url,
							success: (downloadFileRes) => {
								if(downloadFileRes.statusCode===200)
								{
									plus.runtime.install(downloadFileRes.tempFilePath, {},
									 function() {
										uni.showToast({
											title:'更新成功',
											icon:'none'
										})
										plus.runtime.restart()
									}, function(e) {
										uni.showToast({
											title:'更新失败',
											icon:'none'
										})
									})
								}
								
							}
						})
					} 
				}
			});
			
		})
	}) 
	// #endif
}